package com.cyt.web.controller.wx;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.wx.model.po.WxApplyMenu;
import com.cyt.wx.model.vo.WxApplyMenuVo;
import com.cyt.wx.service.WxApplyMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 微信管理-应用菜单控制层
 *
 * @author shengQiang Yu
 * @date 2019-8-9 10:22:14
 */
@RestController
public class WxApplyMenuController extends BaseController {

    @Resource
    private WxApplyMenuService wxApplyMenuService;

    /**
     * 统一跳转
     *
     * @param model 数据模型
     * @return Model
     * @author shengQiang Yu
     * @date 2019-8-9 10:22:14
     */
    @RequestMapping("/wx/wxapplymenu/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 应用菜单列表页面
     *
     * @param applyUseId 应用id
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-9 10:22:14
     */
    @RequestMapping(value = "/wx/wxapplymenu/wxApplyMenuList", method = RequestMethod.GET)
    public ModelAndView wxApplyMenuList(String applyUseId) {
        ModelAndView view = new ModelAndView("/wx/wxapplymenu/wxApplyMenuList");
        view.addObject("applyUseId", applyUseId);
        return view;
    }

    /**
     * 获取菜单集合
     *
     * @param wxApplyMenuVo 对象
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @BaseLog(module = "微信管理", classz = "应用菜单", methods = "菜单List", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapplymenu/list", method = RequestMethod.POST)
    public ReturnResult list(WxApplyMenuVo wxApplyMenuVo) {
        return new ReturnResult(wxApplyMenuService.listExample(wxApplyMenuVo));
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-9 10:22:14
     */
    @BaseLog(module = "微信管理", classz = "应用菜单", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapplymenu/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id, String applyUseId) {
        ModelAndView view = new ModelAndView("/wx/wxapplymenu/wxApplyMenuEdit");
        WxApplyMenu wxApplyMenu = new WxApplyMenu();
        if (StringUtils.isNotBlank(id)) {
            wxApplyMenu = wxApplyMenuService.selectByPrimaryKey(id);
            view.addObject("applyUseId", wxApplyMenu.getApplyUseId());
            if (StringUtils.isNotBlank(wxApplyMenu.getParentId())) {
                WxApplyMenu wxApplyMenuParent = wxApplyMenuService.selectByPrimaryKey(wxApplyMenu.getParentId());
                if (wxApplyMenuParent != null) {
                    wxApplyMenu.setParentName(wxApplyMenuParent.getMenuName());
                }
            }
        } else {
            view.addObject("applyUseId", applyUseId);
        }
        view.addObject(wxApplyMenu);
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request       请求对象
     * @param wxApplyMenuVo 接受参数对象
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-9 10:22:14
     */
    @BaseLog(module = "微信管理", classz = "应用菜单", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapplymenu/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxApplyMenuVo wxApplyMenuVo) {
        return wxApplyMenuService.addOrUpdate(wxApplyMenuVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request       请求对象
     * @param wxApplyMenuVo 接受参数对象
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-8-9 10:22:14
     */
    @BaseLog(module = "微信管理", classz = "应用菜单", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapplymenu/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxApplyMenuVo wxApplyMenuVo) {
        return wxApplyMenuService.remove(wxApplyMenuVo, acceptCurrentUser(request));
    }

    /**
     * 获取菜单树
     *
     * @param wxApplyMenuVo 接受参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-8-13 09:32
     */
    @BaseLog(module = "系统模块", classz = "应用菜单", methods = "菜单树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/wx/wxapplymenu/dTreeList", method = RequestMethod.POST)
    public ReturnResult dTreeList(WxApplyMenuVo wxApplyMenuVo) {
        return wxApplyMenuService.dTreeList(wxApplyMenuVo);
    }
}